package com.ds.infrastructure.hyperspace.container.internal.operator;

import com.ds.infrastructure.hyperspace.common.Operator;

/**
 * usage eg:
 *  [SUB_STRING, startIndex from 0 and inclusive, endIndex from 0 and exclusive]
 * @author  Weishaoying
 */
public class SubStringOperator implements Operator {

    private static final int AGRS_LENGTH = 2;

    @Override
    public String compute(String inputValue, String[] args) {
        if (inputValue == null) {
            return null;
        }
        int startIndex = Integer.parseInt(args[0]);
        if (startIndex > inputValue.length() -1) {
            return null;
        }
        int endIndex = Integer.parseInt(args[1]);
        if (endIndex > inputValue.length() -1) {
            return null;
        }

        return inputValue.substring(startIndex, endIndex);
    }

    @Override
    public String getOperatorName() {
        return "SUB_STRING";
    }

    @Override
    public int getOperatorArgsLength() {
        return AGRS_LENGTH;
    }
}
